package com.yy.ant.sys.controller;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yy.ant.sys.entity.User;
import com.yy.ant.sys.entity.UserRole;
import com.yy.ant.sys.service.IRoleService;
import com.yy.ant.sys.service.IUserRoleService;
import com.yy.ant.sys.service.IUserService;
import com.yy.model.ResultMsg;
import com.yy.model.enums.ResultEnum;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyingying
 * @since 2019-12-20
 */
@RestController
@RequestMapping("/sys/user-role")
public class UserRoleController {
	
	

	/*
	 * @Autowired private IUserService iUserService;
	 * 
	 * 
	 * @Autowired private IRoleService iRoleService;
	 */
	
	@Autowired
	private IUserRoleService iUserRoleService;
	
	/**
	 *授权角色
	 * @return
	 */
	@RequestMapping(value = "/userRole")
	public ResultMsg userRole(@RequestParam(value = "uid")String uid,@RequestParam(value = "rid")String rid) {
		
		//JSONObject jsonObject=JSONObject.parseObject(rid);
		//String ridString=jsonObject.getString("roleid");
		String ridString=rid;
		List<UserRole> list=new ArrayList<UserRole>();
		List<String> list2=new ArrayList<String>();
		String[] id = uid.split(",");
        for (int i = 0; i < id.length; i++) {
        	if (StringUtils.isNotEmpty(id[i])) {
        		list2.add(id[i]);
        		UserRole userRole=new UserRole();
        		userRole.setRoleId(Long.valueOf(ridString));
        		userRole.setUserId(Long.valueOf(id[i]));
        		list.add(userRole);
			}
        }
        //先删除
        iUserRoleService.remove(new QueryWrapper<UserRole>().in("user_id", list2));
        //批量新增
        boolean b= iUserRoleService.saveBatch(list);
		ResultMsg resultMsg=new ResultMsg();
        if (b) {
        	resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_grant_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_grant_upload.getMsg());
		}
		return resultMsg;
	}

	
	
}
